﻿using Anke.SHManage.BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Anke.SHManage.Model;
using Anke.SHManage.Utility;

namespace Anke.SHManage.Web.Areas.DD.Controllers
{
    public class RevisitController : Controller
    {
        //
        // GET: /DD/Revisit/
        //回访主页面
        public ActionResult RevisitList()
        {
            ViewData["txtFillPerson"] = UserOperateContext.Current.Session_UsrInfo.Name;
            return View();
        }
        //新增任务明细
        public ActionResult RevisitAddList()
        {
            return View();
        }
        //新增任务列表
        public ActionResult RevisitAddMain()
        {
            //ViewData["txtFillPerson"] = UserOperateContext.Current.Session_UsrInfo.Name;
            return View();
        }
        //回访操作列表
        public ActionResult RevisitOperateList()
        {
            return View();
        }
        //回访操作查看的列表
        [HttpGet]
        public ActionResult RevisitOperateDetailList(string RowID, int ifOperated) // ifOperate 判断是否有回访操作了
        {
            //string RowID = Convert.ToString(Request.Form["RowID"]);
            // int ifOperate = int.Parse(Request.Form["ifOperate"]);
            // int ifOperate = Convert.ToInt16(Request.Form["ifOperate"]);
            ViewData["RowID"] = RowID;
            ViewData["ifOperate"] = ifOperated;
            return View();
        }
        //回访操作明细(进行回访操作)
        public ActionResult RevisitOperateDetail(string RowID)
        {
            ViewData["RowID"] = RowID;
            ViewData["RevistPerson"] = UserOperateContext.Current.Session_UsrInfo.Name;

            return View();
        }
        //获取操作需回访的内容。
        public ActionResult GetRevistAddDetail()
        {
            string ID = Convert.ToString(Request.Form["ID"]);
            int ifOperate = Convert.ToInt16(Request.Form["ifOperate"]);
         
            int total = 0;
            var list = new RevisitBLL().GetRevisitAddDetail(ref total, ID, ifOperate);

            if (total > 0)
            {
                return Json(new { total = total, rows = list }, "appliction/json", JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { total = 0, rows = "" }, "appliction/json", JsonRequestBehavior.AllowGet);
            }

        }

        #region 提交回访操作内容
        
       
        //提交回访操作内容 更新  I_RevistDetail表，和I_Revisit表
        public ActionResult SaveRevistAddDetail()
        {
            string RevisitDetail = Request.Form["RevisitDetail"].ToString();
            List<I_RevistDetail> relist = JsonHelper.GetJsonInfoBy<List<I_RevistDetail>>(RevisitDetail);
           
            int results = 0;
            I_RevistDetailBLL updata = new I_RevistDetailBLL();
            for (int i = 0; i < relist.Count; i++)
            {
                results = updata.Modify(relist[i], "RevisitState", "Satisfaction", "RevisitWay", "NotSatisfactionReson", "RevistPerson", "RevistTime");
            }


            //回访的主表的ID
            string ReID = relist[0].ID;
            I_Revisit rs = new I_Revisit();
            rs.ID = ReID;
            rs.LastModifier =   relist[0].RevistPerson;  //从前台页面取值，即使Session超时，也不会丢失数据，影响存储。
                
                //UserOperateContext.Current.Session_UsrInfo.Name;

            rs.LastModifyTime = DateTime.Now;

          //  RevisitNumberInfo DeclareInfo = new RevisitNumberInfo();
          //  DeclareInfo.RevisitDeclare = new RevisitBLL().getOldDeclare(ReID);
          //  string tempDeclare = DeclareInfo.RevisitDeclare;  //如果，描述长度大于31，表示有回访记录描述，截取掉，更新新内容。
            string transDeclare = "";
          //  if (tempDeclare.Length > 31)
          //  {
          //      int tempPosition = tempDeclare.IndexOf(";"); //找到分号所在位置，Substring 从0到该位置，成为老的Declare


            //    transDeclare = tempDeclare.Substring(0, tempPosition + 1);
         //   }
          //  else { transDeclare = DeclareInfo.RevisitDeclare; }

            rs.RevisitDeclare = transDeclare + relist[0].RevistPerson +Convert.ToString(DateTime.Now) + "进行回访操作。";


            RevisitNumberInfo numberinfo = new RevisitNumberInfo();
            numberinfo = new RevisitBLL().getRevisitNumber(ReID); //根据ID，取I_RevistDetail中有几个完成，几个未完成，赋值给主表I_Revisit

            if (numberinfo.CompleteNumber == 0)
            {
                rs.UnfinishedNumber = numberinfo.RevistNumber;
            }
            rs.CompleteNumber = numberinfo.CompleteNumber;
            rs.UnfinishedNumber = numberinfo.UnfinishedNumber;


            I_RevisitBLL uprv = new I_RevisitBLL();

            int RVresult = uprv.Modify(rs, "LastModifier", "LastModifyTime", "RevisitDeclare", "CompleteNumber", "UnfinishedNumber");

            if (results > 0 && RVresult > 0)
            {

                return this.JsonResult(Utility.E_JsonResult.OK, "回访信息保存成功！", null, null);
            }
            else
            {
                return this.JsonResult(Utility.E_JsonResult.Error, "回访信息保存失败！", null, null);
            }


        }
        #endregion

        #region 获取回访主表列表


        //获取回访主表列表
        public ActionResult GetRevistMain()
        {
            int rows = int.Parse(Request.Form["rows"]);
            int page = int.Parse(Request.Form["page"]);
            int total = 0;
            var list = new RevisitBLL().GetRevisitMain(page, rows, ref total);

            if (total > 0)
            {
                return Json(new { total = total, rows = list }, "appliction/json", JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { total = 0, rows = "" }, "appliction/json", JsonRequestBehavior.AllowGet);
            }


        }
        #endregion

        #region 挑选任务查询
        
       
        //挑选任务查询  已在SQL中除去了回访表中有的项目
        public ActionResult GetPickTask()
        {
            DateTime TaskDate = Convert.ToDateTime(Request.Form["TaskDate"]);
            string Station = Request.Form["Station"].ToString();
            int CallType = int.Parse(Request.Form["CallType"]);
            int total = 0;
            string eventDispatchCenterID = UserOperateContext.Current.Seesion_Dispatch_SubCenterID; //2021-06-04 gdg 登录人所处急救中心
            var list = new RevisitBLL().GetTaskInfoForRevisitPick(ref total, TaskDate, Station, CallType, eventDispatchCenterID);
            if (total > 0)
            {
                return Json(new { total = total, rows = list }, "appliction/json", JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { total = 0, rows = "" }, "appliction/json", JsonRequestBehavior.AllowGet);
            }

        }
        #endregion

        #region 提交挑选
        
       
        //提交挑选，存入I_Revisit 和I_RevistDetail 库中。
        public ActionResult SaveRevisitAdd()
        {
            string EditRowID = Request.Form["EditRowID"].ToString();//修改项的ID
            DateTime temptaskdate = Convert.ToDateTime(Request.Form["SubmitTaskDate"]);
            string datatimenow = temptaskdate.ToString("yyyyMMdd");
            // string datatimenow = Convert.ToString(DateTime.Now);
            if (EditRowID.Length > 1) { datatimenow = EditRowID; }      //如果EditRowID有值，说明是修改，赋给临时值 datatimenow 作为主表 ID 

            I_Revisit rvinfo = new I_Revisit();
            bool ifnothaddate = true;
            if (EditRowID.Length == 0)
            {

                rvinfo.FillPerson = UserOperateContext.Current.Session_UsrInfo.Name;
                rvinfo.TaskDate = Convert.ToDateTime(Request.Form["SubmitTaskDate"]);
                rvinfo.FillTime = Convert.ToDateTime(Request.Form["FillTime"]);
                rvinfo.ID = datatimenow;
                rvinfo.LastModifier = UserOperateContext.Current.Session_UsrInfo.Name;
                rvinfo.LastModifyTime = Convert.ToDateTime(Request.Form["FillTime"]);
                rvinfo.FillDepartment = UserOperateContext.Current.Session_UsrInfo.P_Department.Name;
                rvinfo.RevisitDeclare = UserOperateContext.Current.Session_UsrInfo.Name + "选择了" + Convert.ToString(Request.Form["SubmitTaskDate"]) + "回访任务;";


                DateTime haddate = Convert.ToDateTime(Request.Form["SubmitTaskDate"]);  //如果新增的日期在主表中已经存在，就不能新插一条数据，用查出的ID 赋给主表ID，修改主表的完成，未完成数量。
                List<I_Revisit> irlist = new RevisitBLL().getRevisitAllDate();

                for (int a = 0; a < irlist.Count; a++)
                {
                    if (haddate == irlist[a].TaskDate)
                    {
                        ifnothaddate = false;
                        datatimenow = irlist[a].ID;
                    }
                }
            }

            rvinfo.CompleteNumber = 0;
            string sDeliveryDetailInfo = Request.Form["sDeliveryDetailInfo"].ToString();
            List<I_RevistDetail> relist = JsonHelper.GetJsonInfoBy<List<I_RevistDetail>>(sDeliveryDetailInfo); //前台传来的从表数据

            I_Revisit secondrvinfo = new I_Revisit(); //当新增时，主表有此条数据，更新完成，未完成。(ID的那一条数据)
            secondrvinfo.ID = datatimenow;

            RevisitNumberInfo rninfotemp = new RevisitBLL().getRevisitNumber(datatimenow);
            secondrvinfo.CompleteNumber = rninfotemp.CompleteNumber;

            secondrvinfo.UnfinishedNumber = rninfotemp.UnfinishedNumber + relist.Count;

            rvinfo.UnfinishedNumber = relist.Count;

            if (EditRowID.Length == 0)
            {
                if (ifnothaddate)
                {
                    new I_RevisitBLL().Add(rvinfo);
                }
                else
                {
                    new I_RevisitBLL().Modify(secondrvinfo, "CompleteNumber", "UnfinishedNumber");

                }
            }

            else
            {
                new I_RevisitBLL().Modify(secondrvinfo, "CompleteNumber", "UnfinishedNumber");

            }

            int results = 0;
            for (int i = 0; i < relist.Count; i++)
            {
                relist[i].ID = datatimenow;
                results = (new I_RevistDetailBLL().Add(relist[i]));  //存入I_RevistDetail附表
            }
            if (results > 0)
            {
                return this.JsonResult(Utility.E_JsonResult.OK, "新增信息成功！", null, null);
            }
            else
            {
                return this.JsonResult(Utility.E_JsonResult.Error, "新增信息失败！", null, null);
            }
        }

        #endregion

        //撤销回访
        public ActionResult deleteRevisit() 
        {
            string ID = Convert.ToString(Request.Form["deleteRowID"]);
            RevisitNumberInfo numberinfo = new RevisitNumberInfo();
            int result = 0;
            int resultz= 0;
            numberinfo = new RevisitBLL().getRevisitNumber(ID); //根据ID，取I_RevistDetail中有几个完成，几个未完成，

            if (numberinfo.CompleteNumber == 0)
            {

                I_RevistDetailBLL dedateDetail = new I_RevistDetailBLL();
                result = dedateDetail.DelBy(p => p.ID == ID);
                I_RevisitBLL dedate = new I_RevisitBLL();

                resultz = dedate.DelBy(p => p.ID == ID);
                if (result > 0 && resultz > 0)
                {
                    return this.JsonResult(Utility.E_JsonResult.OK, "撤销成功！", null, null);
                }
                else
                {
                    return this.JsonResult(Utility.E_JsonResult.Error, "撤销失败！", null, null);
                }
            }
            else {
                return this.JsonResult(Utility.E_JsonResult.Error, "已有被回访数据，不能撤销！", null, null);
            }

            //if ("存在已回访") { 
            //return }

        
        }
    }
}
